/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package main.java.cn.webrx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * <p>Project: jse2204 - AllCodeDemo
 * <p>Powered by webrx On 2022-09-22 16:27:27
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class AllCodeDemo {
    static int num = 0;

    public static void main(String[] args) {
        all(new File("D:\\0824\\javase\\jse2204\\jse2204"));
    }

    public static void all(File dir) {
        if (dir.isDirectory()) {
            File[] fs = dir.listFiles();
            for (File f : fs) {
                if (f.isDirectory()) all(f);
                if (f.isFile() && f.getName().endsWith(".java")) {
                    all(++num, f, new File("src.md"));
                }
            }
        }
        if (dir.isFile() && dir.getName().endsWith(".java")) {
            all(++num, dir, new File("src.md"));
        }
    }


    public static void all(int n, File src, File dst) {
        String template = """
                > %d、%s (%d行，日期：%tF %<tT)
                ```java
                %s
                ```
                                
                """;
        try (var is = new FileInputStream(src); var os = new FileOutputStream(dst, true)) {
            int num = n;
            String s = new String(is.readAllBytes());
            long rows = s.lines().count();
            String path = src.getAbsolutePath();
            String source = String.format(template, num, path, rows, src.lastModified(), s);
            os.write(source.getBytes());
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
